package com.childguo.services.impl;

import com.childguo.Auth.JwtAuth;
import com.childguo.mapper.StuMapper;
import com.childguo.pojo.Stu;
import com.childguo.services.StuService;
import com.childguo.webResult.Result;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

@Service
@Data
@ConfigurationProperties(prefix = "secret_login")
public class StuServicesImpl implements StuService {
    @Autowired
    StuMapper stuMapper;
    @Value("222")
    String secret_;

    @Override
    public Result selectStuById(Integer id) {
        try{
            Stu stu = stuMapper.selectStuById(id);
            return Result.success("SUCCESS", stu);
        }catch (Exception e){
            return Result.fail(e.getMessage(), null);
        }
    }

    @Override
    public Result InsertStu(Stu stu) {
        try{
            int handle_res = stuMapper.insertStu(stu);
            return Result.success("SUCCESS", handle_res);
        }catch (Exception e){
            return Result.fail(e.getMessage().toLowerCase().trim().lastIndexOf("primary")>0?"该用户id已存在":"注册失败", null);
        }
    }

    @Override
    public Result selectStuByStu(Stu stu) {
        try{
            Integer res = stuMapper.selectStuByStu(stu);
            if(res!=null){
                Stu info = stuMapper.selectStuById(stu.getId());
                return Result.success("SUCCESS", new JwtAuth<>().genToken("info",info.getSname()));
            }else{
                return Result.fail("未授权操作",null);
            }
        }catch (Exception e){
            return Result.fail(e.getMessage(), null);
        }
    }

    public static void main(String[] args) {
        System.out.println(new StuServicesImpl().secret_);
    }
}
